
/****** Object:  Stored Procedure [dbo].UsersGetList    Script Date: Thursday, March 18, 2010 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spUsersGetList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[spUsersGetList]
GO

	
			
	
	
/******************************************************************************
**		File: 
**		Name: [dbo].spUsersGetList
**		Desc: 
**
**		Return values:
** 
**		Called by:   
**              
**		Parameters:
**		Input							Output
**     ----------							-----------
**
**		Auth: CodeSmith
**		Date: 3/18/2010 5:03:34 PM
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:				Description:
**		--------		--------				-------------------------------------------
**    
*******************************************************************************/
CREATE PROCEDURE [dbo].spUsersGetList
	@OrderBy varchar(50),
	@OrderDirection varchar(5),
	@Page int,
	@PageSize int,
	@TotalRecords int output
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

CREATE TABLE #TempUsers (
	RowNumber INT IDENTITY (1, 1) NOT NULL,
	Mobile nvarchar(20)	
)

DECLARE @sql nvarchar(2000)
DECLARE @Top int

SET @Top = @Page*@PageSize
IF @PageSize > 0
   SET ROWCOUNT @Top
-- insert primary keys into temp table
SET @sql =	N'INSERT INTO #TempUsers ([Mobile]) SELECT '
SET @sql = @sql + ' [Mobile] FROM [dbo].[Users]  ORDER BY [' + @OrderBy + N'] ' + @OrderDirection
EXEC (@sql)
SET ROWCOUNT 0

SELECT @TotalRecords = COUNT(*) FROM [Users]

SELECT
	[dbo].[Users].[Mobile],
	[Password],
	[Email],
	[Ten],
	[Ho],
	[NgSinh],
	[Phai],
	[DChi],
	[NhanMail],
	[NhanSMS],
	[ThanhPho],
	[Quan],
	[NgonNgu],
	[NguoiGioiThieu],
	[MaSoCaNhan]
FROM
	#TempUsers AS tblTemp JOIN [dbo].[Users] ON
	tblTemp.Mobile = [dbo].[Users].Mobile 	
WHERE (@PageSize = 0) OR (@PageSize > 0 AND (@Page - 1)*@PageSize < RowNumber AND RowNumber <= @Page*@PageSize)
ORDER BY RowNumber

DROP TABLE #TempUsers

GO

	

